<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车管理</title>
    <link href="/static/libs/bootstrap.css" rel="stylesheet">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        .navbar {
            background-color: #ff5000;
            color: white;
            padding: 10px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .navbar .logo {
            display: flex;
            align-items: center;
        }
        .navbar .logo span {
            margin-left: 10px;
        }
        .navbar a {
            color: white;
            text-decoration: none;
            margin: 0 10px;
        }
        .search-bar {
            display: flex;
            justify-content: center;
            margin: 20px 0;
        }
        .search-bar input {
            width: 500px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px 0 0 5px;
        }
        .search-bar button {
            padding: 10px;
            background-color: #ff5000;
            border: none;
            color: white;
            border-radius: 0 5px 5px 0;
        }
        .category-menu {
            background-color: #f5f5f5;
            padding: 10px;
            display: flex;
            justify-content: space-around;
        }
        .category-menu a {
            text-decoration: none;
            color: #333;
            padding: 10px 20px;
        }
        .content-box {
            padding: 20px;
        }
        .left-nav, .right-content {
            float: left;
        }
        .left-nav {
            width: 20%;
            background-color: #f5f5f5;
            padding: 10px;
        }
        .left-nav a {
            display: block;
            text-decoration: none;
            color: #333;
            padding: 10px 20px;
            margin: 5px 0;
            background-color: #fff;
            border-radius: 5px;
        }
        .left-nav a.active, .left-nav a:hover {
            background-color: #ff5000;
            color: white;
        }
        .right-content {
            width: 75%;
            margin-left: 5%;
        }
        .right-title {
            font-size: 24px;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input, .form-group select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .btn-submit {
            padding: 10px 20px;
            background-color: #ff5000;
            border: none;
            color: white;
            border-radius: 5px;
            cursor: pointer;
        }
        .btn-submit:hover {
            background-color: #e04c00;
        }
        .address-list {
            margin-top: 30px;
        }
        .address-list table {
            width: 100%;
            border-collapse: collapse;
        }
        .address-list th, .address-list td {
            padding: 10px;
            border: 1px solid #ccc;
            text-align: left;
        }
        .address-list th {
            background-color: #f5f5f5;
        }
    </style>
    <style>
        .content {
            display: flex;
            flex-direction: column;
        }
    </style>
</head>
<body>
<div class="navbar">
    <div class="logo">机械工业</div>
    <div class="nav-links">
        <a id="index1Link" href="#">首页</a>
        <a id="cartLink" href="#">购物车</a>

    </div>
</div>

<div class="search-bar">
    <input type="text" placeholder="搜索商品、店铺">
    <button>搜索</button>
</div>

<div class="content-box clear_fix">
    <div class="left-nav">
        <a id="userMessageLink" href="#">收货地址管理</a>
        <a id="orderlistLink" href="#" >我的订单</a>
        <a href="#" class="active">我的购物车</a>
        <a id="information_modificationLink" href="#">个人资料</a>
        <a id="password_modificationLink" href="#">修改密码</a>
    </div>

    <div class="content" id="content">
        <div class="btn btn-danger" style="width: 140px" id="clear-cart">清空购物车</div>
        <table class="table">
            <thead>
            <tr>
                <th scope="col">商品名</th>
                <th scope="col">商品单价</th>
                <th scope="col">商品数量</th>
                <th scope="col">操作</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>

</div>
</body>
<script src="/static/libs/bootstrap.js"></script>
<script src="/static/js/axios.js"></script>

<script>

    function getUserIdFromUrl() {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get('id');
    }
    const userId = getUserIdFromUrl();
    const userMessageLink = document.getElementById("userMessageLink");
    const index1Link = document.getElementById("index1Link");
    const cartLink = document.getElementById("cartLink");
    const orderlistLink = document.getElementById("orderlistLink");
    const information_modificationLink = document.getElementById("information_modificationLink");
    const password_modificationLink = document.getElementById("password_modificationLink");
    userMessageLink.href = `userMessage?id=${userId}`;
    index1Link.href = `index1?id=${userId}`;
    cartLink.href =`cart?id=${userId}`;
    orderlistLink.href = `orderlist?id=${userId}`;
    information_modificationLink.href = `information_modification?id=${userId}`;
    password_modificationLink.href = ` password_modification?id=${userId}`;


    const remove = id => {
        axios.delete("http://localhost:8080/trolleys/delete?id=" + Number.parseInt(id)).then(res => {
            window.location.reload();
        });
    }

    onload = () => {

        document.querySelector("#clear-cart").addEventListener("click", e => {
            axios.post("http://localhost:8080/trolleys/clear?id="+userId).then(res => {
                window.location.reload();
            });
        });

        axios.get("http://localhost:8080/trolleys/list?id="+userId).then(res => {
            if (res.data.length === 0) {
                const template = `<div style="display: flex; align-items: center; justify-content: center; height: 300px;">
                     您的购物车为空
                     <div class="btn btn-primary" style="margin-left: 10px" onclick="window.location.href = './index1?id=${userId}'">去商城</div>
                        </div>`;

                const dom = document.createElement("div");
                dom.innerHTML = template;
                document.querySelector(".content#content").style.display = "none";
                document.querySelector(".left-nav").insertAdjacentElement("afterend", dom)
                return;
            }
            res.data.forEach(item => {
                let template = `
        <tr>
            <th scope="row">{{productName}}</th>
            <td>￥{{productPrice}}</td>
            <td>
                <input type="number" value="{{quantity}}">
            </td>
            <td>
               <div class="btn btn-warning" onclick="redirectToPayment('{{id}}', '{{productPrice}}', '{{productName}}')">结算</div>
                <div class="btn btn-danger" onclick="remove({{id}})">删除</div>
            </td>
        </tr>
        `
                template = template.replace(/{{productName}}/g, item.productName);
                template = template.replace(/{{productPrice}}/g, item.productPrice);
                template = template.replace(/{{quantity}}/g, item.quantity);
                template = template.replaceAll(/{{id}}/g, item.id);
                let tr = document.createElement("tr");
                tr.innerHTML = template;

                tr.querySelector("input[type='number']").addEventListener("change", e => {
                    const value = e.target.value;
                    axios.put("http://localhost:8080/trolleys/changeQuantity?id=" + item.id + "&quantity=" + value).then(() => {
                        window.location.reload();
                    });
                })

                document.querySelector(".table tbody").appendChild(tr);
            });

        })
    }
    function redirectToPayment(orderId, price, title) {
        const url = `/payment?orderid=${orderId}&price=${price}&title=${encodeURIComponent(title)}`;
        window.location.href = url;
    }
</script>
</html>
